package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.R;
import com.itheima.pojo.Employee;
import com.itheima.service.IEmployeeService;
import com.itheima.utils.BaseContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLIntegrityConstraintViolationException;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    IEmployeeService employeeService;

    //用户登录
    @PostMapping("/login")
    private R login(@RequestBody Employee employee, HttpServletRequest request){
        //1.获取name和password
        String password = employee.getPassword();
        //使用md5解码
        password = DigestUtils.md5DigestAsHex(password.getBytes());

        //2.查数据库
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(employee.getUsername()!=null && employee.getUsername() !=""
                ,Employee::getUsername,employee.getUsername());
        //3.调用service查询
        Employee findEmployee = employeeService.getOne(wrapper);

        //4.判断查询出来的是否存在 findEmployee == "" &&
        if (findEmployee == null){
            return R.error("管理员不存在,登录失败");
        }

        if (!findEmployee.getPassword().equals(password)){
            return R.error("密码错误,登录失败");
        }

        //判断状态
        if (findEmployee.getStatus() == 0){
            return R.error("登录失败");
        }

        //5.保存到session
        request.getSession().setAttribute("employee",findEmployee.getId());

        log.info(String.valueOf(findEmployee.getId()));
        return R.success(findEmployee);
    }

    //用户退出
    @PostMapping("/logout")
    private R logout(HttpServletRequest request){
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }


    //新增用户
    @PostMapping()
    private R addEmployee(@RequestBody Employee employee,HttpServletRequest request){
        //由于密码是md5加密的，所有需要进行加密(指定密码)
        //String password = employee.getPassword();
        String password = DigestUtils.md5DigestAsHex("123456".getBytes());

        //查询赋值
        employee.setPassword(password);

        //封装其它数据(mp的帮忙封装了)

        //调用方法(捕获异常)

        employeeService.save(employee);

        return R.success("新增员工成功");
    }


    //分页查询(请求参数放到了请求行中，直接接收)
    @GetMapping("/page")
    private R pageSelect(Long page,Long pageSize,String name){
        //分页查询
        IPage<Employee> p1 = new Page<>(page,pageSize);

        //模糊查询
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(name!=null && name != "",Employee::getUsername,name);

        employeeService.page(p1,wrapper);


        return R.success(p1);
    }


    //员工的禁用和启用
    @PutMapping
    private R updateStatus(@RequestBody Employee employee,HttpServletRequest request){

        employeeService.updateById(employee);

        return R.success("修改成功");
    }

    //编辑员工--查询数据并返回
    @GetMapping("/{id}")
    private R getEmployee(@PathVariable Long id){
        //先查询数据库中是否存在
        Employee byId = employeeService.getById(id);

        if (byId==null){
            return R.error("该员工不存在");
        }
        return R.success(byId);
    }
}
